package com.bea.ftd.dashboard.controller;

import java.util.ArrayList;
import java.util.Date;
import java.util.List;

import com.bea.ftd.common.dao.ApiDefineRepo;
import com.bea.ftd.common.dao.MsgInConfRepo;
import com.bea.ftd.common.dao.MsgMappingRepo;
import com.bea.ftd.common.dao.MsgOutConfRepo;
import com.bea.ftd.common.entity.ApiDefine;
import com.bea.ftd.common.entity.MsgInConf;
import com.bea.ftd.common.entity.MsgOutConf;
import com.google.gson.Gson;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.RestController;

import com.bea.ftd.common.ValidationException;
import com.bea.ftd.common.entity.MsgMapping;
import com.bea.ftd.dashboard.domain.AjaxResponse;
import com.bea.ftd.dashboard.enums.ResponseStatus;
import com.bea.ftd.dashboard.service.IMsgMappingService;

@RestController
@RequestMapping("/msgMapping")
public class MsgMappingController {
	private Logger LOGGER = LoggerFactory.getLogger(MsgMappingController.class);

	@Autowired
	private IMsgMappingService msgMappingServiceImpl;

	@Autowired
	MsgInConfRepo msgInConfRepo;

	@Autowired
	MsgOutConfRepo msgOutConfRepo;

	@Autowired
	MsgMappingRepo msgMappingRepo;

	@Autowired
	ApiDefineRepo apiDefineRepo;

	@RequestMapping("/listAllInterfaceId")
	public List<ApiDefine> listAllInterfaceId(){
		return apiDefineRepo.findAll();
	}

	@RequestMapping("/listAll")
	public List<MsgMapping> listAllMsgMappings() {
		return msgMappingServiceImpl.getAllMsgMappings();
	}

	@RequestMapping("/getMsgOutConf")
	public List<MsgOutConf> listAllMsgOutConf(String apiId){

		return msgOutConfRepo.findAllByApiId(apiId);
	}

	@RequestMapping("/getMsgInConf")
	public List<MsgInConf> listAllMsgInConf( String apiId){

		return msgInConfRepo.findAllByApiId(apiId);
	}

	@RequestMapping("/saveOrUpdate")
	public AjaxResponse addMsgMapping(MsgMapping msgMapping) {
		LOGGER.info("Save or update msgMapping - " + msgMapping.toString());
		try {
			msgMappingServiceImpl.addMsgMapping(msgMapping);
			return new AjaxResponse(ResponseStatus.SUCCESS);
		} catch (ValidationException e) {
			LOGGER.error("输入参数错误：" + e.getErrorMessage());
			return new AjaxResponse(ResponseStatus.FAIL, e.getErrorMessage());
		} 
		catch (Exception e) {
			LOGGER.error("Error in saving msgMapping to DB. " + e.getMessage());
			return new AjaxResponse(ResponseStatus.FAIL, e.getMessage());
		}

	}

	@RequestMapping("/saveOrEditMsgMappingList")
	public AjaxResponse saveOrEditMsgMappingList(String mappingList,String apiId){
		System.out.println(mappingList);
		Gson gson = new Gson();
		List<String> list = gson.fromJson(mappingList, new ArrayList().getClass());
		List<String> resList = new ArrayList<>();
		for(String one:list){
			if(one.contains("<")){

			}else{
				resList.add(one);
			}
		}
		List<MsgOutConf> msgOutList = msgOutConfRepo.findAllByApiId(apiId);
		List<MsgInConf> msgInConfs = msgInConfRepo.findAllByApiId(apiId);

		if(msgOutList.size()>0&&msgInConfs.size()>0){
			//msgMappingRepo.deleteByInterfaceId(apiId);
		}
		int i=0;
		for(MsgOutConf msgOutOne:msgOutList){
			MsgMapping msgMapping = new MsgMapping();
			msgMapping.setOutMsgId(msgOutOne.getId());
			msgMapping.setOutMsgParamName(msgOutOne.getParamName());

			for(MsgInConf msgInConf:msgInConfs){
				if(msgInConf.getParamName().equals(resList.get(i))){
					msgMapping.setInMsgId(msgInConf.getId());
				}

			}
			msgMapping.setInterfaceId(apiId);
			msgMapping.setInMsgParamName(resList.get(i));
			msgMapping.setCreateTime(new Date());
			msgMapping.setIsActivated((long)1);
			msgMapping.setUpdateTime(new Date());
			i++;
			if(msgMapping.getInMsgId()!=null){
				msgMappingRepo.save(msgMapping);
			}

		}

		return new AjaxResponse(ResponseStatus.SUCCESS);
	}

}
